(ns pe.p28)

(defn matrix-diagonals [msize]
  {:pre (odd? msize)}
  (loop [size 1 last-num 1 result []]
    (cond
      (> size msize) result
      (= 1 size) (recur 3 1 [1])
      :else
        (let [step (dec size)
              ln (+ last-num (* step 4))
              diags (for [num (range last-num (inc ln) step) :when (not (= last-num num))] num)]
          (recur (+ size 2) ln (concat result diags))))))

(println (reduce + (matrix-diagonals 1001)))